﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Seagull.BarTender.Print;
using System.Diagnostics;

namespace POSPlus.BTxmlWriter
{
    public class BarTenderXMLWriter
    {
/*
#region MyRegion
		
        private Dictionary<String, String> _xmlScript;
        private Dictionary<String, String> _command;
        private Dictionary<String, String> _print;
        private Dictionary<String, String> _format;
        private Dictionary<String, String> _recordSet;
        private String _formatLocation;
        private String _fieldDelimiter;
        private int _delimitation;
        private bool _useFieldNamesFromFirstRecord;
        private List<String> _textData;

        private String _fileLocation;
        private XmlTextWriter _xmlWriter;

        public const int DelimMixedQuoteAndComma = 1;
        public const int DelimQuoteAndComma = 2;
        public const int DelimComma = 4;
        public const int DelimTab = 8;
        public const int DelimCustom = 16;


        public BarTenderXMLWriter()
        {
            _xmlScript = new Dictionary<string, string>(3);
            _xmlScript.Add("Version", "");
            _xmlScript.Add("Name", "");
            _xmlScript.Add("ID", "");

            _command = new Dictionary<string, string>(2);
            _command.Add("Name", "");
            _command.Add("RepeatCount", "");

            _print = new Dictionary<string, string>(7);
            _print.Add("JobName", "");
            _print.Add("WaitForJobToComplete", "");
            _print.Add("Timeout", "");
            _print.Add("ReturnPrintData", "");
            _print.Add("ReturnSummary", "");
            _print.Add("ReturnLabelData", "");
            _print.Add("ReturnChecksum", "");

            _format = new Dictionary<string, string>(2);
            _format.Add("CloseAtEndOfJob", "");
            _format.Add("SaveAtEndOfJob", "");

            _recordSet = new Dictionary<string, string>(3);
            _format.Add("Name", "");
            _format.Add("Type", "");
            _format.Add("AddIfNone", "");

        }

        public void WriteXML(String fileLocation)
        {
            _xmlWriter = new XmlTextWriter(fileLocation, Encoding.UTF8);
            _xmlWriter.WriteStartDocument();
            _xmlWriter.WriteStartElement("XMLScript");
            foreach (KeyValuePair<String, String> attribute in _xmlScript)
            {

            }
            _xmlWriter.WriteEndElement();
            _xmlWriter.WriteEndDocument();
        }



        public void WriteXML(String fileLocation)
        {
            _xmlWriter = new XmlTextWriter(fileLocation, Encoding.UTF8);
            _xmlWriter.WriteStartDocument();
            _xmlWriter.WriteStartElement("Student");
            _xmlWriter.WriteStartAttribute("Scott");
            _xmlWriter.WriteString("cool");
            _xmlWriter.WriteEndAttribute();
            _xmlWriter.WriteStartElement("r");
            _xmlWriter.WriteStartElement("Name", "");
            _xmlWriter.WriteString("Studend");
            _xmlWriter.WriteEndElement();
            _xmlWriter.WriteStartElement("Address");
            _xmlWriter.WriteString("Colony");
            _xmlWriter.WriteEndElement();
            _xmlWriter.WriteEndElement();
            _xmlWriter.WriteEndDocument();
            _xmlWriter.Close();
        } 
	#endregion
        */
        public XmlTree XMLTree { get; private set; }
        public XmlTextWriter XMLWriter { get; private set; }

        public BarTenderXMLWriter()
        {
            XMLTree = new XmlTree();
            XMLWriter = new XmlTextWriter(@"C:\test.xml", Encoding.UTF8);
            XMLWriter.WriteStartDocument();

            // XML Script Node
            XMLTree.AddNode(XMLTree.CreateNode(XmlNodeType.Element, "XMLScript"));
            XMLTree.AddAttribute("/XMLScript", XMLTree.CreateAttribute("Version"));

            // Command
            XMLTree.AddNode("/XMLScript", XMLTree.CreateNode(XmlNodeType.Element, "Command"));
            XMLTree.AddAttribute("/XMLScript/Command", XMLTree.CreateAttribute("Name"));

            // Print
            XMLTree.AddNode("/XMLScript/Command", XMLTree.CreateNode(XmlNodeType.Element, "Print"));

            // Format
            XMLTree.AddNode("/XMLScript/Command/Print", XMLTree.CreateNode(XmlNodeType.Element, "Format"));

            // RecordSet
            XMLTree.AddNode("/XMLScript/Command/Print", XMLTree.CreateNode(XmlNodeType.Element, "RecordSet"));
            XMLTree.AddAttribute("/XMLScript/Command/Print/RecordSet", XMLTree.CreateAttribute("Name"));
            XMLTree.AddAttribute("/XMLScript/Command/Print/RecordSet", XMLTree.CreateAttribute("Type"));

            // Delimitation
            XMLTree.AddNode("/XMLScript/Command/Print/RecordSet", XMLTree.CreateNode(XmlNodeType.Element, "Delimitation"));
            XMLTree.SetInnerText("/XMLScript/Command/Print/RecordSet/Delimitation", "btDelimQuoteAndComma");

            // UseFieldNamesFromFirstRecord
            XMLTree.AddNode("/XMLScript/Command/Print/RecordSet", XMLTree.CreateNode(XmlNodeType.Element, "UseFieldNamesFromFirstRecord"));
            XMLTree.SetInnerText("/XMLScript/Command/Print/RecordSet/UseFieldNamesFromFirstRecord", "true");

            // TextData
            XMLTree.AddNode("/XMLScript/Command/Print/RecordSet", XMLTree.CreateNode(XmlNodeType.Element, "TextData"));
            XMLTree.SetInnerText("/XMLScript/Command/Print/RecordSet/TextData", "<![CDATA[\n\"Description\"\n\"Test\"]]>");


            XMLTree.Document.WriteTo(XMLWriter);
            XMLWriter.WriteEndDocument();
            XMLWriter.Close();

            /*
            XMLTree = new Tree();
            XMLWriter = new XmlTextWriter(@"C:\test.xml", Encoding.UTF8);
            XMLWriter.WriteStartDocument();
            // XML Script Node
            XmlNode xmlScript = XMLTree.Document.CreateNode(XmlNodeType.Element, "XMLScript", null);
            XmlAttribute attribute = XMLTree.Document.CreateAttribute("Version");
            attribute.Value = "2.0";
            xmlScript.Attributes.Append(attribute);

            // Command Node
            XmlNode command = XMLTree.Document.CreateNode(XmlNodeType.Element, "Command", null);
            attribute = XMLTree.Document.CreateAttribute("Name");
            command.Attributes.Append(attribute);

            // Print Node
            XmlNode print = XMLTree.Document.CreateNode(XmlNodeType.Element, "Print", null);

            // Format Node
            XmlNode format = XMLTree.Document.CreateNode(XmlNodeType.Element, "Format", null);

            // RecordSet Node
            XmlNode recordSet = XMLTree.Document.CreateNode(XmlNodeType.Element, "RecordSet", null);
            recordSet.Attributes.Append(XMLTree.Document.CreateAttribute("Name"));
            recordSet.Attributes.Append(XMLTree.Document.CreateAttribute("Type"));
            
            // Delimitation Node
            XmlNode delimitation = XMLTree.Document.CreateNode(XmlNodeType.Element, "Delimitation", null);

            // UseFieldNamesFromFirstRecord
            XmlNode useFieldNamesFromFirstRecord = XMLTree.Document.CreateNode(XmlNodeType.Element, "UseFieldNamesFromFirstRecord", null);
            useFieldNamesFromFirstRecord.InnerText = "true";

            XmlNode textData = XMLTree.Document.CreateNode(XmlNodeType.Element, "TextData", null);


            
            /*
            // Test Lines
            command.Attributes["Name"].Value = "Job1";
            command.Attributes.Append(XMLTree.Document.CreateAttribute("RepeatCount"));
            command.Attributes["RepeatCount"].Value = "1";
            print.Attributes.Append(XMLTree.Document.CreateAttribute("WaitForJobToComplete"));
            print.Attributes["WaitForJobToComplete"].Value = "true";
            print.Attributes.Append(XMLTree.Document.CreateAttribute("ReturnPrintData"));
            print.Attributes["ReturnPrintData"].Value = "true";
            print.Attributes.Append(XMLTree.Document.CreateAttribute("ReturnSummary"));
            print.Attributes["ReturnSummary"].Value = "true";
            format.InnerText = @"C:\test.btw";
            recordSet.Attributes["Name"].Value = "Text File 1";
            recordSet.Attributes["Type"].Value = "btTextFile";
            delimitation.InnerText = "btDelimQuoteAndComma";
            textData.InnerText = "<![CDATA[\"Description\"\n\"Test Item One\"\n\"Test Item Two\"\n\"Three\"\n\"Four\"]]>";
            */
            /*
            print.AppendChild(format);
            print.AppendChild(recordSet);
            recordSet.AppendChild(delimitation);
            recordSet.AppendChild(useFieldNamesFromFirstRecord);
            recordSet.AppendChild(textData);
            command.AppendChild(print);
            xmlScript.AppendChild(command);
            XMLTree.AddNode(xmlScript);
            XMLTree.Document.WriteTo(XMLWriter);
            XMLWriter.WriteEndDocument();
            XMLWriter.Close();
            */

            /*
            Process proc = new Process();
            proc.StartInfo.FileName = @"E:\Program Files\Seagull\BarTender Suite\BarTend.exe";
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.Arguments = "/XMLScript=\"C:\\test.xml";
            proc.Start();

            using (Engine engine = new Engine())
            {
                engine.Start();
                string response = engine.XMLScript(@"C:\test.xml", XMLSourceType.ScriptFile);
                Console.WriteLine(response);
                engine.Stop();
            }
            proc.Close();
             */
        }
    }
}
